package algorithm.leetcode.simple;

/*
 给定一个 N 叉树，返回其节点值的前序遍历
 */

import java.util.ArrayList;
import java.util.List;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020/11/25
 */
public class Question589 {

    public static void main(String[] args) {

    }

    public List<Integer> preorder(Node root) {
        List<Integer> res = new ArrayList<>(10);
        preTraverse(root, res);
        return res;
    }

    private void preTraverse(Node node, List<Integer> res) {
        if (node == null) {
            return;
        }
        res.add(node.val);
        for (int i = 0; i < node.children.size(); i++) {
            // 递归每个孩子结点
            preTraverse(node.children.get(i), res);
        }
    }


    private static class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int val) {
            this.val = val;
        }

        public Node(int val, List<Node> children) {
            this.val = val;
            this.children = children;
        }
    }
}
